﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.gametime;

namespace l1cs.server.utils
{
    public class TimePeriod
    {
        private static Logger _log = Logger.getLogger( typeof( TimePeriod ).FullName );

        private TimeSpan _timeStart;
        private TimeSpan _timeEnd;

        public TimePeriod( TimeSpan timeStart, TimeSpan timeEnd )
        {
            if ( timeStart.Equals( timeEnd ) )
            {
                throw new ArgumentException( "timeBegin must not equals timeEnd" );
            }

            _timeStart = timeStart;
            _timeEnd = timeEnd;
        }

        private bool includes( L1GameTime time, TimeSpan timeStart, TimeSpan timeEnd )
        {
            TimeSpan when = time.toTime();
            return timeStart.CompareTo( when ) <= 0 && 0 < timeEnd.CompareTo( when );
        }

        public bool includes( L1GameTime time )
        {
            /*
             * 分かりづらいロジック・・・ timeStart after timeEndのとき(例:18:00~06:00)
             * timeEnd~timeStart(06:00~18:00)の範囲内でなければ、
             * timeStart~timeEnd(18:00~06:00)の範囲内と見なせる
             */
            return _timeStart < _timeEnd  ? !includes( time, _timeEnd,
                    _timeStart ) : includes( time, _timeStart, _timeEnd );
        }
    }
}
